package com.xjcs.saplingplan.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.OvershootInterpolator;

import androidx.appcompat.widget.AppCompatImageView;

public class DraggableImageView extends AppCompatImageView {
    private float lastX, lastY;
    private int parentWidth, parentHeight;
    private boolean isDragging = false;
//    private float snapThreshold = 50f; // 吸附阈值(像素)
    private float dragThreshold = 10f; // 拖动触发阈值(像素)
    private float overshootFactor = 0.5f; // 回弹强度

    public DraggableImageView(Context context) {
        super(context);
        init();
    }

    public DraggableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DraggableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setClickable(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        ViewParent parent = getParent();
        if (parent instanceof ViewGroup) {
            ViewGroup parentView = (ViewGroup) parent;
            parentWidth = parentView.getWidth();
            parentHeight = parentView.getHeight();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getRawX();
        float y = event.getRawY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                isDragging = false;
                break;

            case MotionEvent.ACTION_MOVE:
                float deltaX = x - lastX;
                float deltaY = y - lastY;

                // 判断是否达到拖动阈值
                if (!isDragging && (Math.abs(deltaX) > dragThreshold || Math.abs(deltaY) > dragThreshold)) {
                    isDragging = true;
                }

                if (isDragging) {
                    float newX = getX() + deltaX;
                    float newY = getY() + deltaY;

                    // 边界检查并添加弹性效果
                    if (newX < 0) {
                        newX = -overshootFactor * newX;
                    } else if (newX > parentWidth - getWidth()) {
                        newX = parentWidth - getWidth() - overshootFactor * (newX - (parentWidth - getWidth()));
                    }

                    if (newY < 0) {
                        newY = -overshootFactor * newY;
                    } else if (newY > parentHeight - getHeight()) {
                        newY = parentHeight - getHeight() - overshootFactor * (newY - (parentHeight - getHeight()));
                    }

                    setX(newX);
                    setY(newY);

                    lastX = x;
                    lastY = y;
                }
                break;

            case MotionEvent.ACTION_UP:
                if (isDragging) {
                    checkSnapToEdge();
                } else {
                    performClick();
                }
                isDragging = false;
                break;
        }
        return true;
    }

    private void checkSnapToEdge() {
        float currentX = getX();
        float currentY = getY();
        float centerX = currentX + getWidth() / 2f;

        // 检查是否靠近左右边缘
        if (centerX < parentWidth / 2f) {
            // 靠近左边
//            if (currentX < snapThreshold) {
                snapToEdge(0, currentY);
//            }
        } else {
            // 靠近右边
//            if (currentX > parentWidth - getWidth() - snapThreshold) {
                snapToEdge(parentWidth - getWidth(), currentY);
//            }
        }
    }

    private void snapToEdge(float targetX, float currentY) {
        // 限制Y坐标在有效范围内
        float targetY = Math.max(0, Math.min(currentY, parentHeight - getHeight()));

        // 使用弹性动画
        animate()
                .x(targetX)
                .y(targetY)
                .setDuration(300)
                .setInterpolator(new OvershootInterpolator(1.5f))
                .start();
    }

    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }
}